<template>
    <div class="layout-padding-auto">
    <el-table :data="state.dataList" v-loading="state.loading" border style="width: 100%;height: 100%;"
              :cell-style="tableStyle.cellStyle" :header-cell-style="tableStyle.headerCellStyle"
              @selection-change="selectionChangHandle"
              @sort-change="sortChangeHandle">
        <el-table-column prop="attribute1"  :label="$t('poheader.poCode')" show-overflow-tooltip  width="230"/>
        <el-table-column prop="materialCode"  :label="$t('so.materialcode')" show-overflow-tooltip  width="230"/>
        <el-table-column prop="materialName" :label="$t('so.materialname')" show-overflow-tooltip  width="230"/>
        <el-table-column prop="materialSpec" :label="$t('so.materialspec')" show-overflow-tooltip  width="330"/>
        <el-table-column prop="unit" :label="$t('relationD.unit')"  show-overflow-tooltip  width="170"/>
        <el-table-column prop="earliestRequireDate" :label="$t('srmAsnD.deliveryDate')"  show-overflow-tooltip  width="230"/>
        <el-table-column prop="asnQty" :label="$t('srmAsnD.deliveryNoteQty')" show-overflow-tooltip width="170">
            <template #default="scope">
                <!--根据不同的状态选择不同的颜色的tag-->
                <el-tag size="large" type='success' >
                    {{ scope.row.asnQty }}
                </el-tag>
            </template>
        </el-table-column>
      <!--        <el-table-column prop="returnQty" :label="$t('srmAsnD.ReturnedQuantityBeforeWarehousing')" show-overflow-tooltip width="170"/>-->
        <el-table-column prop="deliveryNoteNum" :label="$t('srmAsnD.actualDeliveryQty')" show-overflow-tooltip width="170">
            <template #default="scope">
                <!--根据不同的状态选择不同的颜色的tag-->
                <el-tag size="large" type='success'  v-if="scope.row.deliveryNoteNum">
                    {{ scope.row.deliveryNoteNum }}
                </el-tag>
            </template>
        </el-table-column>
        <el-table-column prop="remark" :label="$t('srmAsnD.remark')" show-overflow-tooltip width="230"/>
        <el-table-column  fixed="right" :label="$t('common.action')" width="150">
            <template #default="scope">
                <el-button icon="el-icon-search" v-if="formdata.status != 'toAudit'"  type="primary" @click="queryPrint(scope.row)">{{ $t('common.queryBtn') }}</el-button>
                <el-button icon="el-icon-edit" v-if="formdata.status == 'toAudit'" plain type="primary" @click="queryEdit(scope.row)">{{ $t('common.editBtn') }}</el-button>
            </template>
        </el-table-column>
    </el-table>
    <pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" v-bind="state.pagination"/>
</div>
  <!-- 编辑、新增  -->
    <FormDialog ref="formDialogRef" @refresh="getDataList(false)"/>
    <EditDialog ref="editDialogRef" @refresh="getDataList(false)"/>
</template>

<script setup lang="ts" name="systemSrmPodetail">
import {BasicTableProps, useTable} from "/@/hooks/table";
import {getPage} from "/@/api/platform/srm/srmAsnH";

const FormDialog = defineAsyncComponent(() => import('./form.vue'));
const EditDialog = defineAsyncComponent(() => import('./edit.vue'));

// 定义变量内容
const formDialogRef = ref()
const editDialogRef = ref()
// 多选变量
const selectObjs = ref([]) as any
const multiple = ref(true)
// 搜索变量
const queryRef = ref()

// 父级点击的行
const parentClickRow = {}

const props = defineProps({
    rowList: {
        type: Object,
    },
})

onMounted (()=>{
    query(props.rowList)
})

const state: BasicTableProps = reactive<BasicTableProps>({
    queryForm: {},
    pageList: getPage
})

const formdata = reactive({
    status:'',
    asnCode:'',
    asnQty:'',
    asnHId:'',
});

// 清空搜索条件
const resetQuery = () => {
    // 清空搜索条件
    queryRef.value?.resetFields()
    getDataList(false)
}

//  table hook
const {
    getDataList,
    currentChangeHandle,
    sizeChangeHandle,
    sortChangeHandle,
    tableStyle
} = useTable(state)

// 通过id进行子表的查询
const query = (row: Object) => {
    formdata.status = row.status
    formdata.asnCode = row.asnCode
    formdata.asnQty = row.sumQty
    formdata.asnHId = row.id
    // 赋值查询参数
    state.queryForm.masterId = row.id
    // 保存选中的行数据
    Object.assign(parentClickRow, row)
    // 刷新子表数据
    getDataList()
}

// 多选事件
const selectionChangHandle = (objs: { id: string }[]) => {
    selectObjs.value = objs.map(({id}) => id);
    multiple.value = !objs.length;
};

// 暴露变量
defineExpose({
    resetQuery,
    query
});

//明细查询
const queryPrint = (row) =>{
    //打开弹框
    formDialogRef.value.openDialog(row.id,formdata.asnCode,formdata.asnQty,formdata.asnHId,row.materialCode)
}

//明细编辑
const queryEdit = (row) =>{
    //打开弹框
    editDialogRef.value.openDialog(row.id,formdata.asnCode,formdata.asnQty,formdata.asnHId)
}

</script>